#!/bin/bash
   
type pgrep > /dev/null || exit 1
[ 2 -gt $# ] && echo -e "\tUsage:$0 {start|stop} {cmd}\n" && exit 1
 
cd $(dirname $0)/../
WATCH_LOG=/var/log/watch_proc.log
WATCH_NAME=$(basename $0)
OPERATION=$1
shift
PROC_CMD="$*"
PROC_NAME=$(basename ${PROC_CMD%% *})
PROC_ARGS="${PROC_CMD#*$PROC_NAME}"

function StopProc {
    local cpids=$(pgrep -P $1)
    local cpid=
    printf "%${2}s|-$1\n"
    [ -f /proc/$1/stat ] && kill $1
    local n=$(expr 2 + $2)
    for cpid in $cpids; do
        StopProc "$cpid" $n
    done
}

case $OPERATION in
    start)
        pid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>$PROC_ARGS$")
        pid=$(echo -e "$pid"|grep -v "^$$$")
        [ -n "$pid" ] && echo "WATCH on \"$PROC_CMD\" is runing with PID:$pid" && exit 2
        while :; do
            $PROC_CMD > /dev/null 2>/dev/null &
            pid=$!
            while sleep 4; do [ -f /proc/$pid/stat ] || break; done
            echo "$(date +'%F %T') \"$PROC_CMD\" quit exceptionally!" >> $WATCH_LOG
            StopProc "$pid" 0 >> $WATCH_LOG
            echo "$(date +'%F %T') \"$PROC_CMD\" restart." >> $WATCH_LOG
        done > /dev/null 2>/dev/null & ;;
    stop)
        ppid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>$PROC_ARGS$")
        [ -z "$ppid" ] && echo "No WATCH on \"$PROC_CMD\" !" && exit 1
        pid=$(pgrep -lP $ppid|grep -v '^[0-9]\+ \+sleep$'|awk '{print $1}')
        [ -z "$pid" ] && echo "No process: \"$PROC_CMD\"" && kill -9 $ppid && exit 0
        kill -9 $ppid
        echo "$(date +'%F %T') \"$PROC_CMD\" quit manually." >> $WATCH_LOG
        StopProc "$pid" 0
        exit 0 ;;
    *)
        echo -e "\tUsage:$0 start|stop cmd argv ...\n" ;;
esac

